package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.os.Message;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.adapter.DischargeSummaryAdapter;
import com.hxkcloud.nurse.beans.DischargeSummaryBean;
import com.hxkcloud.nurse.beans.DischargeWholeBean;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullableRecyclerView;

import java.util.ArrayList;
import java.util.List;

/*
* 出院小结
* */
public class DischargeSummaryActivity extends BaseActivity implements View.OnClickListener, PullToRefreshLayout.OnRefreshListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discharge_summary);
        patientId = getIntent().getStringExtra("patientId");
        initView();
        initListener();
        initData();
    }

    private LinearLayout ll_department_empty;
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView tv_add;
    private String title = "出院小结";

    private PullToRefreshLayout refreshLayout;
    private PullableRecyclerView recycleView;
    private DischargeSummaryAdapter dischargeSummaryAdapter;
    private List<DischargeSummaryBean> dischargeSummaryBeanList;
    private int pageIndex = 1;
    private int pageSize = 15;
    private String scaleId = null;
    private String questionId = null;
    private DischargeWholeBean resultBean;
    private int totalCount = 0;
    private String patientId;
    private static final int FINISH_CODE = 10;

    @Override
    protected void updateByHandler(Message msg) {
        if (pageIndex == 1) {
            refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
        } else {
            refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
        }
        switch (msg.what) {
            case MessageCode.NETWORKCODE_CONNECT_ERROR:
                toastMsg("网络错误!");
                break;
            case MessageCode.GET_DISCHARGE_SUMMARY_SUCCESS:
                if (msg.obj != null) {
                    resultBean = (DischargeWholeBean) msg.obj;
                    totalCount = resultBean.getTotalCount();
                    if (resultBean.getList().size() == 0) {
                        toastMsg("没有更多了");
                    } else {
                        if (pageIndex == 1) {
                            dischargeSummaryBeanList.clear();
                        }
                        dischargeSummaryBeanList.addAll(resultBean.getList());
                        if (dischargeSummaryBeanList.size() > 0) {
                            ll_department_empty.setVisibility(View.INVISIBLE);
                        } else {
                            ll_department_empty.setVisibility(View.VISIBLE);
                        }
                        dischargeSummaryAdapter.notifyDataSetChanged();
                    }
                }
                break;
            case MessageCode.GET_DISCHARGE_SUMMARY_FAILURE:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                } else {
                    toastMsg("拉取数据失败");
                }
                break;


        }
        dismisssLoading();
    }

    @Override
    protected void initView() {
        ll_department_empty = (LinearLayout) findViewById(R.id.ll_department_empty);
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        tv_add = (TextView) findViewById(R.id.tv_title_func);
        tv_add.setText("添加");


        // pushRecordBeanList = new ArrayList<>();
        refreshLayout = (PullToRefreshLayout) findViewById(R.id.refreshlayout_assessrecordlist);
        recycleView = (PullableRecyclerView) findViewById(R.id.recyclerview_assessrecordlist);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(this);
        tv_title.setText(title);
        tv_add.setVisibility(View.VISIBLE);
        tv_add.setOnClickListener(this);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(mLayoutManager);
        dischargeSummaryBeanList = new ArrayList<>();
    /*    for (int i = 0; i < 2; i++) {
            DischargeSummaryBean dischargeSummaryBean = new DischargeSummaryBean();
            dischargeSummaryBeanList.add(dischargeSummaryBean);
        }*/
        dischargeSummaryAdapter = new DischargeSummaryAdapter(dischargeSummaryBeanList, this);
        dischargeSummaryAdapter.setOnItemClickListener(new DischargeSummaryAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                DischargeSummaryBean dischargeSummaryBean = dischargeSummaryBeanList.get(position);
                Intent intent = new Intent(DischargeSummaryActivity.this, UpDischargeSummaryActivity.class);
                intent.putExtra("patientId", patientId);
                intent.putExtra("dischargeSummaryId", dischargeSummaryBean.getDischargeSummaryId());
                intent.putExtra("data", dischargeSummaryBean);
                startActivityForResult(intent, FINISH_CODE);
            }
        });
        refreshLayout.setOnRefreshListener(this);
        recycleView.setAdapter(dischargeSummaryAdapter);

    }

    @Override
    protected void initListener() {
    }

    @Override
    protected void initData() {

        YiJiaHuNetWorkUtils.getDischargeSummary(patientId, pageIndex, pageSize, handler);
        loading();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.tv_title_func:
                Intent intent = new Intent(DischargeSummaryActivity.this, UpDischargeSummaryActivity.class);
                intent.putExtra("patientId", patientId);
                startActivityForResult(intent, FINISH_CODE);
                break;
        }
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        pageIndex = 1;
        initData();
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        if (dischargeSummaryBeanList.size() == totalCount) {
            toastMsg("没有更多了");
            refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            return;
        }
        pageIndex++;
        initData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (FINISH_CODE == requestCode) {
            if (resultCode == FINISH_CODE) {
                pageIndex = 1;
                initData();
            }

        }
    }
}
